package lanou.a36ke_01.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;

import cn.bmob.v3.Bmob;
import lanou.a36ke_01.R;
import lanou.a36ke_01.adapters.mineadapter.MineAdapter;
import lanou.a36ke_01.adapters.mineadapter.MineListAdapter;
import lanou.a36ke_01.beanlists.newsbean.NewListBean;
import lanou.a36ke_01.tools.BaseActivity;
import lanou.a36ke_01.fragmengs.discoveryfragments.DiscoveryFragment;
import lanou.a36ke_01.fragmengs.equityfragments.EquityFragment;
import lanou.a36ke_01.fragmengs.minefragments.MineFragment;
import lanou.a36ke_01.fragmengs.newsfragments.NewsFragment;

//1.News 新闻  2.equity 投资  3.Discovery 发现  4.Message 消息  5.Mine 我的

//继承于BaseActivity 使用基类
public class MainActivity extends BaseActivity {

    private ArrayList<Fragment> fragments;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private DrawerLayout drawerLayout;
    private ListView listView;
    private MineListAdapter listAdapter;
    private LinearLayout layout;

    @Override
    protected int setLayout() {

        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        viewPager = bindView(R.id.main_view_pager);
        tabLayout = bindView(R.id.main_tab_layout);
        drawerLayout = bindView(R.id.dl_main_acty);
        listView = bindView(R.id.main_list_view);
        layout = bindView(R.id.main_ll_drawer);

    }

    @Override
    protected void initDate() {
        //默认初始化
        Bmob.initialize(this,"5449e6d82b621e954c9c544fa95b55f1");
        initTab();
        initDraVp();
        initDraList();
    }

    protected void initTab() {
        fragments = new ArrayList<>();
        fragments.add(new NewsFragment());
        fragments.add(new EquityFragment());
        fragments.add(new DiscoveryFragment());
        fragments.add(new MineFragment());
        MineAdapter adapter = new MineAdapter(getSupportFragmentManager());
        adapter.setFragments(fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        //设置显示内容的模式
        tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        //改变选中时横条的颜色
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);
        //设置滑动时选中文字的颜色
        tabLayout.setTabTextColors(Color.rgb(0x98, 0x9d, 0xab), Color.rgb(0x6c, 0x8f, 0xff));
        //为tablayout设置图片
        tabLayout.getTabAt(0).setIcon(R.drawable.selector_news);
        tabLayout.getTabAt(1).setIcon(R.drawable.selector_equity);
        tabLayout.getTabAt(2).setIcon(R.drawable.selector_discovery);
        tabLayout.getTabAt(3).setIcon(R.drawable.selector_mine);
    }

    //为抽屉设置点击事件的方法
    public void Onclicker() {
        drawerLayout.openDrawer(Gravity.LEFT);
    }

    //开启抽屉的方法 手势滑动
    public void open() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); //打开手势滑动
    }

    //关闭手势滑动的方法
    public void close() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //关闭手势滑动
    }

    protected void initDraVp() {
        //判断可以显示抽屉的viewpager
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {

                if (position == 0) {
                    open();
                } else {
                    close();
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
    protected void initDraList() {
        layout.bringToFront();
        ArrayList<NewListBean> list = new ArrayList<>();
        list.add(new NewListBean(R.mipmap.black, "全部"));
        list.add(new NewListBean(R.mipmap.green, "早期项目"));
        list.add(new NewListBean(R.mipmap.blue, "B轮后"));
        list.add(new NewListBean(R.mipmap.dagongsi, "大公司"));
        list.add(new NewListBean(R.mipmap.ziben, "资本"));
        list.add(new NewListBean(R.mipmap.red, "深度"));
        list.add(new NewListBean(R.mipmap.yellow, "研究"));
        list.add(new NewListBean(R.mipmap.pink, "氪TV"));
        listAdapter = new MineListAdapter(this);
        listAdapter.setArrayList(list);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //用广播传值
                Intent intent = new Intent();

                intent.setAction("lanou.36ke_01");
                switch (position) {
                    case 0:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=all&pagingAction=up");
                        break;
                    case 1:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=67&pagingAction=up");
                        break;
                    case 2:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=68&pagingAction=up");
                        break;
                    case 3:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=23&pagingAction=up");
                        break;
                    case 4:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=69&pagingAction=up");
                        break;
                    case 5:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=70&pagingAction=up");
                        break;
                    case 6:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=71&pagingAction=up");
                        break;
                    case 7:
                        intent.putExtra("key", "https://rong.36kr.com/api/mobi/news?pageSize=20&columnId=tv&pagingAction=up");
                        break;
                }
                sendBroadcast(intent);
                drawerLayout.closeDrawers();
            }
        });
    }
}
